package com.resou.reader.crash;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.resou.reader.MainActivity;
import com.resou.reader.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/***
 *
 */
public class CrashHandlerActivity extends AppCompatActivity {

    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSend)
    TextView tvSend;
    @BindView(R.id.mTitleBar)
    RelativeLayout mTitleBar;
    @BindView(R.id.tvInfo)
    TextView tvInfo;
    @BindView(R.id.btnClose)
    Button btnClose;
    @BindView(R.id.btnCopy)
    Button btnCopy;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    String errorInfo = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crash_handler);
        ButterKnife.bind(this);
        ivBack.setVisibility(View.GONE);
        tvTitle.setText("异常捕获");
        errorInfo = getIntent().getStringExtra("errorInfo");
        tvInfo.setText(errorInfo + "");
    }

    @OnClick({R.id.btnClose, R.id.btnCopy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnClose:
                startActivity(new Intent(this, MainActivity.class));
                break;
            case R.id.btnCopy:
                copyTextToSystem(this, tvInfo.getText().toString());
                break;
        }
    }

    /**
     * 复制文本到系统剪切板
     */
    public static void copyTextToSystem(Context context, String text) {
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData mClipData = ClipData.newPlainText("Label", text);
        cm.setPrimaryClip(mClipData);
        Toast.makeText(context, "复制成功", Toast.LENGTH_SHORT).show();
    }

}
